//
//  JCReportProModel.m
//  SquashSpark
//
//  Created by 郭吉成 on 2017/12/1.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "JCReportProModel.h"
#import "NSString+Extension.h"

@implementation JCReportProModel

+ (JCReportProModel *)creatModelWith:(NSDictionary *)sourceDic{
    JCReportProModel *proModel = [JCReportProModel new];
    proModel.foreHand = [NSString checkIfNullWithString:sourceDic[@"ForeHand"]];
    proModel.backHand = [NSString checkIfNullWithString:sourceDic[@"BackHand"]];
    proModel.foreHandPer = [NSString checkIfNullWithString:sourceDic[@"ForeHandPer"]];
    proModel.backHandPer = [NSString checkIfNullWithString:sourceDic[@"BackHandPer"]];
    proModel.weekData = [NSMutableArray array];
    [proModel.weekData addObjectsFromArray:[JCReportProSubData creatWeekDataWith:sourceDic[@"WeekData"]]];
    proModel.gradeReference = [JCReportProSubData creatModelWith:sourceDic[@"GradeReference"] isNeedCom:YES];
    proModel.grade = [JCReportProSubData creatModelWith:sourceDic[@"Grade"] isNeedCom:NO];
    return proModel;
}

@end







@implementation JCReportProSubData

+ (JCReportProSubData *)creatModelWith:(NSDictionary *)sourceDic isNeedCom:(BOOL)isNeedCom{
    JCReportProSubData *data = [JCReportProSubData new];
    if (isNeedCom) {
        if(![YFUserDefaults generalMetricSystemUnit]){
            data.avgSpeed = [NSString stringWithFormat:@"%.1f",[Common kmhToMph:round([NSString checkIfNullWithString:sourceDic[@"AvgSpeed"]].integerValue)]];
        }else{
            data.avgSpeed = [NSString checkIfNullWithString:sourceDic[@"AvgSpeed"]];
        }
    }else{
        data.avgSpeed = [NSString checkIfNullWithString:sourceDic[@"AvgSpeed"]];
    }
    data.sweetRate = [NSString checkIfNullWithString:sourceDic[@"SweetRate"]];
    data.avgStrength = [NSString checkIfNullWithString:sourceDic[@"AvgStrength"]];
    return data;
}

+ (NSArray <JCReportProSubData *> *)creatWeekDataWith:(NSDictionary *)sourceDic{
    NSMutableArray *arry = [NSMutableArray array];
    NSArray *keys = sourceDic.allKeys;
     keys = [keys sortedArrayUsingComparator:^NSComparisonResult(NSString  * key1, NSString  * key2) {
        return [key1 compare:key2];
    }];
    for (NSString *key in keys) {
        NSDictionary *dic = sourceDic[key];
        [arry addObject:[JCReportProSubData creatModelWith:dic isNeedCom:YES]];
    }
    return arry.mutableCopy;
}

@end
